package com.itheima.movement.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.itheima.common.ThreadLocalUtil;
import com.itheima.entity.dto.MovementCommentDto;
import com.itheima.entity.pojo.MovementComment;
import com.itheima.entity.pojo.MovementCommentLike;
import com.itheima.entity.vo.MovementCommentLikeVo;
import com.itheima.entity.vo.MovementCommentVo;
import com.itheima.movement.mapper.MovementCommentMapper;
import com.itheima.movement.service.MovementCommentLikeService;
import com.itheima.movement.service.MovementCommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author :wujunjie
 * createTime :2022-12-23 14:32
 * description :
 */
@Service
public class MovementCommentServiceImpl implements MovementCommentService {
    @Autowired
    private MovementCommentMapper movementCommentMapper;
    @Autowired
    private MovementCommentLikeService movementCommentLikeService;

    @Override
    public List<MovementCommentVo> selectByMovementIds(List<String> movementIdList) {
        List<MovementComment> movementComments = movementCommentMapper.selectByMovementIds(movementIdList);
        List<MovementCommentVo> movementCommentVos = BeanUtil.copyToList(movementComments, MovementCommentVo.class);
        //todo 需要将喜欢的数据也查出来封装成Vo
        Set<String> ids = movementCommentVos.stream().filter(Objects::nonNull).map(MovementCommentVo::getId).collect(Collectors.toSet());
        List<String> idList=new ArrayList<>(ids);
        List<MovementCommentLikeVo> movementCommentLikeVos=movementCommentLikeService.seleteByMovementCommentIds(idList);
        Map<String, List<MovementCommentLikeVo>> map = movementCommentLikeVos.stream().collect(Collectors.groupingBy(MovementCommentLikeVo::getMovementCommentId));
        for (MovementCommentVo movementCommentVo : movementCommentVos) {

            movementCommentVo.setLikes(map.get(movementCommentVo.getId()));
        }
        return movementCommentVos;
    }

    @Override
    public Boolean insertComment(MovementCommentDto movementCommentDto) {

        MovementComment movementComment = new MovementComment();
        BeanUtils.copyProperties(movementCommentDto,movementComment);
        movementComment.setId(IdUtil.getSnowflakeNextIdStr());
        movementComment.setCreateTime(LocalDateTime.now());
        movementComment.setUserId(ThreadLocalUtil.getId());
        return movementCommentMapper.insertComment(movementComment);


    }

    @Override
    public Boolean deleteComment(String id) {
        MovementComment movementComment = new MovementComment();
        movementComment.setId(id);
        movementComment.setIsDeleted(LocalDateTime.now());

        return movementCommentMapper.updateIsDeletedById(movementComment);
    }

}
